package com.cheetah.start.common.filter;

import cn.hutool.http.ContentType;
import com.cheetah.start.common.wrapper.MyRequestWrapper;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
 * 过滤器
 *
 * @author xjh
 * @date 2022/01/10
 */
@Component
public class JsonFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        String contentType = request.getContentType();
        if (Objects.equals(contentType, ContentType.JSON.getValue())) {
            MyRequestWrapper requestWrapper = new MyRequestWrapper((HttpServletRequest) request);
            filterChain.doFilter(requestWrapper, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }
}
